<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%
	String idGry = request.getParameter("game_id");
	int game_id = Integer.parseInt(idGry);
%>
<%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%
	SessionFactory sessionFactory = HibernateUtil
			.getSessionAnnotationFactory();
	Session s = sessionFactory.openSession();
	s.beginTransaction();
	UserMethods um = new UserMethods();

	GameMethods gm = new GameMethods();
	response.setContentType("text/html");
	Game g = gm.idToGame(sessionFactory, s, game_id);
	FeedbackMethods fb = new FeedbackMethods();
	List<GameFeedback> listfb = fb.listGameFeedbacks(sessionFactory, s,
			game_id);
	int commentsCount = fb.countFbByGameId(sessionFactory, s, game_id);
	Integer user = (Integer) session.getAttribute("user_id");
%>

<div class="page-header">
	<h2 class="page-title">
		Nasze gry -
		<%=g.getName()%></h2>
</div>
<div id="content">
	<br />

	<table>

		<tr>
			<td>
				<div class="col-md-4 col-sm-6" style="width: 95%;">
					<div class="team-member">
						<div class="member-thumb">
							<!--	<img src="images/team3.jpg" alt=""> -->
						</div>
						<div class="member-infos">
							<h4 class="member-name">
							<img src="games/gameAvatar.jsp?game_id=<%=game_id%>" alt="GameAvatar" style="height: 250px">
			<br> <br>
								<%
									out.println("<a class=\"games\" id=\"" + game_id + "\" href=\"#\">"
											+ g.getName() + "</a>");
								%>
								
								<%
									if (user != null) {

										if (gm.checkIfLike(sessionFactory, s, user, game_id).size() > 0) {
								%>
								<form role="form" id='like' action="../deleteGameLike"
									method="POST">
									<input type="hidden" name="game_id" id="game_id"
										value="<%=game_id%>" /> <input type="hidden" id="user"
										name="user" value="<%=user%>" /> <input type="submit"
										class="mainBtn" value="Usuń z ulubionych" />
								</form>
								<%
									} else {
								%>

								<form role="form" id='like' action="../addGameLike"
									method="POST">
									<input type="hidden" name="game_id" id="game_id"
										value="<%=game_id%>" /> <input type="hidden" id="user"
										name="user" value="<%=user%>" /> <input type="submit"
										class="mainBtn" value="Dodaj do ulubionych" />
								</form>
								<%
									}
									}
								%>
							</h4>
							<span class="member-role"> <%
 	RateMethods rm = new RateMethods();
 	System.out.println(rm.returnAverageRateByGame(sessionFactory, s,
 			game_id));
 	out.println("Ogólna ocena uzytkownikow: "
 			+ rm.returnAverageRateByGame(sessionFactory, s, game_id));
 %> <br /> <%
 	out.println("Komentarzy: " + commentsCount);
 %>
							</span>

							<%
								String opis1 = null;
								if (g.getDescribe() == null || g.getDescribe().equals("0"))
									opis1 = "Brak";
								else
									opis1 = g.getDescribe();
							%>
							<p class="member-desc">
								<%
									out.println("Opis gry: " + opis1);
								%>
							</p>
							<br>
							<%
								GameCategoryMethods gcat = new GameCategoryMethods();
								List<String> gamecategories = new ArrayList<String>();
								gamecategories = gcat.listCategoriesByGame(sessionFactory, s,
										game_id);
								if (gamecategories == null)
									out.println("Gra nie ma jeszcze zadnej kategorii");
								else if (gamecategories.size() > 0) {
									out.println("Kategorie: ");
									for (String gc : gamecategories) {
										out.println(gc + ", ");
									}
								}
							%>
						</div>
					</div>
				</div>
			</td>

		</tr>
	</table>
	<%
		if (user != null) {
	%>
	<form role="form" id='addGameRate' action="../addGameRate"
		method="POST">
		<input type="hidden" name="game_id" id="game_id" value="<%=game_id%>" />
		<input type="hidden" id="user" name="user" value="<%=user%>" />
		Ocena: <select id="rate" name="rate">
			<option value="0">0</option>
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="4">4</option>
			<option value="5">5</option>
		</select> <input type="submit" class="mainBtn" value="Oceń" />
	</form>
	<%
		} else
			out.println("Oceny moga wystawiac tylko zalogowani uzytkownicy");
	%>
	<br>
	<div class="page-header">
		<h2 class="page-title" style="color: white;">
			Komentarze (<%=commentsCount%>)
		</h2>
	</div>



	<form role="form" id='editGame' action="../addGameFeedback"
		method="POST">
		<p class="member-desc" style="color: white;">
			<input type="hidden" name="game_id" id="game_id" value="<%=game_id%>" />
			Jako: <input type="hidden" id="user" name="user" value="<%=user%>" />
			<%
				if (user != null) {
					User u = um.id_to_user(sessionFactory, s, user);
					out.print(u.getUsername());
				} else {
					out.print("Anonim");
				}
			%>
			<br>
			<textarea name="comment" rows="3" class="form-control" id="comment"
				placeholder="Tresc komentarza"></textarea>
			<input type="submit" class="mainBtn" value="Dodaj komentarz" />
		</p>
	</form>
	<p class="member-desc" style="color: white;">
		<%
			if (listfb.size() > 0) {
				for (GameFeedback fb1 : listfb) {
		%>

		<%
			String userName = null;
					if (!fb1.getUser_id().equals("null")) {
						String user_id = fb1.getUser_id();
						User u = um.id_to_user(sessionFactory, s,
								Integer.parseInt(user_id));
						userName = u.getUsername();
					} else
						userName = "Anonim";
					String comment = fb1.getComment();
					String add_date = fb1.getAdd_date();
		%>
	
	<table>
		<form role="form" id='deleteFb' action="../deleteGameFeedback"
			method="POST">
			<input type="hidden" id="comment_id" name="comment_id"
				value="<%=fb1.getId()%>" />
			<%
				if (user != null) {
							if (um.id_to_user(sessionFactory, s, user)
									.getUsername().equals(userName)) {
			%><input type="submit" class="mainBtn" value="Usuń" />
			<%
				}
						}
			%>
		</form>
		<tr>
			<td>Uzytkownik:</td>
			<td>
				<%
					out.println(userName);
				%>
			</td>
		</tr>
		<tr>
			<td>Data dodania:</td>
			<td>
				<%
					out.println(add_date);
				%>
			</td>
		</tr>
		<tr>
			<td>Tresc komentarza:</td>
			<td>
				<%
					out.println(comment);
				%>
			</td>
		</tr>
	</table>
	<%
		out.println("<br />");
			}

		} else if (listfb.size() == 0)
			out.println("Ta gra nie ma jeszcze zadnych komentarzy");
	%>

</div>




<%
	s.getTransaction().commit();
%>


